package pw.hais.etgsh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

import pw.hais.etgsh.R;
import pw.hais.etgsh.activity.AuthorActivity;
import pw.hais.etgsh.activity.FeedbackActivity;
import pw.hais.etgsh.adapter.YuYinSpinnerAdapter;
import pw.hais.etgsh.app.BaseFragment;
import pw.hais.etgsh.app.V;
import pw.hais.utils.L;
import pw.hais.utils.SPUtils;

/**
 * 设置
 * Created by hais1992 on 2016/1/23/023.
 */
public class SettingFragment extends BaseFragment implements CompoundButton.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener, View.OnClickListener, AdapterView.OnItemSelectedListener {
    private CheckBox checkbox_bfyy, checkbox_zdfy;
    private Spinner spinner_yy;
    private SeekBar seekbar_sd;
    private Button btn_hf;
    private YuYinSpinnerAdapter adapter;

    private View layout_yjfk,layout_gyzz;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_setting, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        layout_yjfk = view.findViewById(R.id.layout_yjfk);
        layout_gyzz = view.findViewById(R.id.layout_gyzz);
        layout_yjfk.setOnClickListener(this);
        layout_gyzz.setOnClickListener(this);

        btn_hf = (Button) view.findViewById(R.id.btn_hf);
        checkbox_bfyy = (CheckBox) view.findViewById(R.id.checkbox_bfyy);
        checkbox_zdfy = (CheckBox) view.findViewById(R.id.checkbox_zdfy);
        spinner_yy = (Spinner) view.findViewById(R.id.spinner_yy);
        seekbar_sd = (SeekBar) view.findViewById(R.id.seekbar_sd);
        seekbar_sd.setMax(100);

        adapter = new YuYinSpinnerAdapter(YuYinSpinnerAdapter.getSpinnerData());
        spinner_yy.setAdapter(adapter);
        spinner_yy.setOnItemSelectedListener(this);

        checkbox_bfyy.setOnCheckedChangeListener(this);
        checkbox_zdfy.setOnCheckedChangeListener(this);
        seekbar_sd.setOnSeekBarChangeListener(this);
        btn_hf.setOnClickListener(this);
        loadDataToView();
    }

    private void loadDataToView() {
        checkbox_bfyy.setChecked(SPUtils.getObject(V.SP_IS_AUTO_START, Boolean.class, false));
        checkbox_zdfy.setChecked(SPUtils.getObject(V.SP_IS_AUTO_NEXT, Boolean.class, false));
        spinner_yy.setSelection(SPUtils.getObject(V.SP_IS_YUYIN_RENWU_NUNBER, Integer.class, 0));
        seekbar_sd.setProgress(SPUtils.getObject(V.SP_LANGDU_SUDU, Integer.class, 50));
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.getId() == R.id.checkbox_bfyy) {
            SPUtils.saveObject(V.SP_IS_AUTO_START, isChecked);
        }
        if (buttonView.getId() == R.id.checkbox_zdfy){
            SPUtils.saveObject(V.SP_IS_AUTO_NEXT, isChecked);
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        SPUtils.saveObject(V.SP_LANGDU_SUDU, progress);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_hf:
                SPUtils.delObject(V.SP_IS_AUTO_START);
                SPUtils.delObject(V.SP_IS_AUTO_NEXT);
                SPUtils.delObject(V.SP_LANGDU_SUDU);
                spinner_yy.setSelection(0);
                loadDataToView();
                L.showShort("设置已恢复");
                break;
            case R.id.layout_yjfk:
                startActivity(new Intent(context, FeedbackActivity.class));
                break;
            case R.id.layout_gyzz:
                startActivity(new Intent(context, AuthorActivity.class));
                break;
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        SPUtils.saveObject(V.SP_IS_YUYIN_RENWU,adapter.getItem(position).code);
        SPUtils.saveObject(V.SP_IS_YUYIN_RENWU_NUNBER, position);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
